package org.sunsys.spitterapp.controller;


import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import org.sunsys.spitterapp.model.LoginPageModel;
import org.sunsys.spitterapp.model.ServiceResponse1;
import org.sunsys.spitterapp.model.ServiceResponse2;

@Controller
public class LoginController {

	public LoginController() {
	}
	
	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/login	
	 * e.g.:
	 * 		http://localhost:8080/spitterapp/spitter/login
	 * 
	 * 	.	Login.html can be used to post the request
	 * 	.	userName parameter should be part of the request (either in URL or in the request body) to get matched this method
	 * 
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * 
	 * @param userName
	 * @param password
	 * @return
	 */
	@RequestMapping(value="/login", method=RequestMethod.POST, params="userName")
	public @ResponseBody String handleLogin(@RequestParam String userName, @RequestParam String password) {
		System.out.println("User Name:"+userName);
		System.out.println("password:"+password);
		return "LoginSuccess";
	}
	
	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/login2
	 * e.g.:
	 * 		http://localhost:8080/spitterapp/spitter/login2
	 * 
	 * 	.	Login2.html can be used to post the request
	 * 	.	userName parameter should be part of the request (either in URL or in the request body) to get matched this method
	 * 	.	all LoginPageModel will be populated with request paramter (userName and password by spring)
	 * 
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param loginPageModel
	 * @return
	 */
	@RequestMapping(value="/login2", method=RequestMethod.POST, params="userName")
	public @ResponseBody String handleLogin(LoginPageModel loginPageModel) {
		System.out.println("User Name:"+loginPageModel.getUserName());
		System.out.println("password:"+loginPageModel.getPassword());
		return "LoginSuccess";
	}
	
	
	
	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/login3
	 * e.g.:
	 * 		http://localhost:8080/spitterapp/spitter/login3
	 * 
	 * 	.	Login3.html can be used to post the request
	 * 	.	entire data which comes as part of the Http body will be assigned to the method parameter
	 * 
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param loginPageModel
	 * @return
	 */
	@RequestMapping(value="/login3", method=RequestMethod.POST)
	public @ResponseBody String handleLogin(@RequestBody String data) {
		System.out.println("data from http body:"+data);
		return "LoginSuccess";
	}

	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/handleLoginWithRequestAndResponseEntity
	 * e.g.:
	 * 		http://localhost:8080/spitterapp/spitter/handleLoginWithRequestAndResponseEntity
	 * 
	 * 	.	Use Postmaster to send request.
	 * 	.	using HttpEntity - we can get the request headers, body 
	 * 	.	Using ResponseEntity - we can send headers, body and status
	 * 
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param loginPageModel
	 * @return
	 */
	@RequestMapping(value="/handleLoginWithRequestAndResponseEntity", method=RequestMethod.POST, consumes="text/html" )
	public ResponseEntity<String> handleLoginWithRequestAndResponseEntity(HttpEntity<String> httpEntity) {
		
		System.out.println("handleLogin	With	RequestEntity And	ResponseEntity");
		
		System.out.println("does request has http body:"+httpEntity.hasBody());
		HttpHeaders httpHeaders = httpEntity.getHeaders();
		
		System.out.println("Http Header content type:"+httpHeaders.getContentType());
		System.out.println("local header"+httpHeaders.getLocation());

		/* 
		 * we can't add parameters to the HttpHeader object, which is received from request HttpEntity object 
		 * i.e.: To the request Headers, we can't add the response headers parameters. Hence create new instance of HttpHeaders
		 */
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("headerVariable2", "value1");
		
//		ResponseEntity<String> responseEntity = new ResponseEntity<String>(HttpStatus.OK);
		/*
		 * While creating the ResponseEntity - we can specify HttpBody, Headers, Status 
		 */
		ResponseEntity<String> responseEntity = new ResponseEntity<String>("Http Response body- success",responseHeaders,  HttpStatus.OK);
		
		
		return responseEntity;
	}
	
	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/handleLoginWithModelAttribute
	 * e.g.:
	 * 		http://localhost:8080/spitterapp/spitter/handleLoginWithModelAttribute
	 *
	 * 	.	
	 * 
	 * 
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param loginPageModel
	 * @return
	 */
	@RequestMapping(value="/handleLoginWithModelAttribute", method=RequestMethod.POST)
	public @ResponseBody String handleLoginWithModelAttribute(@ModelAttribute LoginPageModel loginPageModel) {
		System.out.println("handleLogin With Model Attribute");
		
		System.out.println("User Name:"+loginPageModel.getUserName());
		System.out.println("password:"+loginPageModel.getPassword());
		return "LoginSuccess";
	}
	
	
	/**
	 * (RESTFUL web service)
	 * 
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/handleWebServiceRequest1
	 * e.g.:
	 * 		http://localhost:8080/spitterapp/spitter/handleWebServiceRequest1
	 * 
	 * 	<dependency>
	 * 			<groupId>com.sun.xml.bind</groupId>
	 * 			<artifactId>jaxb-impl</artifactId>
	 * 			<version>2.2.5</version>
	 * 	</dependency>
	 * 
	 * 	.	"Postman" is Chrome extension
	 * 	.	"Postman" can be used to send REST request 
	 * 	. 	after placing jaxb-impl in the pom.xml, package-info.java is consider by JAXB in spring while marshalling the returned object
	 * 	.	ServiceResponse1 is annotated with @XmlRootElement
	 * 	.	   
	 * 
	 * response:
	 * =========
	 * 	<spi:response xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:spi="www.sunsys.org/spitterapp">
	 * 		<spi:status>sucess</spi:status>
	 * 		<spi:timeTaken>some minits</spi:timeTaken>
	 * 		</spi:response>
	 * 
	 * 
	 * Note:
	 * =====
	 * 		schema location is not part of the request xml
	 * 
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param data String
	 * @return
	 */
	@RequestMapping(value="/handleWebServiceRequest1", method=RequestMethod.POST)
	public @ResponseBody ServiceResponse1 handleWebServiceRequest1(@RequestBody String data ) {
		System.out.println("handleWebServiceRequest1");
		System.out.println("parameter or http request body:"+data);
		ServiceResponse1 response = new ServiceResponse1();
		response.setTimeTaken("some minits");
		response.setStatus("sucess");
		return response;
	}
	
	/**
	 * (RESTFUL web service)
	 * 
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/handleWebServiceRequest2
	 * e.g.:
	 * 		http://localhost:8080/spitterapp/spitter/handleWebServiceRequest2
	 * 
	 * 	.	Login4.html can be used to post the request
	 * 	.	ServiceResponse is annotated with @XmlRootElement
	 * 	.	spring converts the ServiceResponse (returned) obj  to xml, since its annotated with @XmlRootElement  
	 * 
	 * 
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param data String
	 * @return
	 */
	@RequestMapping(value="/handleWebServiceRequest2", method=RequestMethod.POST)
	public @ResponseBody ServiceResponse2 handleWebServiceRequest2(@RequestBody String data ) {
		
		//TODO this is not working.
		//TODO should learn HttpMessageConverter concept and OXM topics
		
		System.out.println("handleWebServiceRequest2");
		System.out.println("parameter or http request body:"+data);
		ServiceResponse2 response = new ServiceResponse2();
		response.setTimeTaken("some minits");
		response.setStatus("sucess");
		return response;
	}

	
	
}
